//
//  CampusListTypeHeaderView.m
//  ShowYork
//
//  Created by hxh on 2021/3/25.
//  Copyright © 2021 hxh. All rights reserved.
//

typedef void(^doneBlock)(NSInteger index);
#import "CampusListTypeHeaderView.h"

@interface CampusListTypeHeaderView ()
@property (nonatomic,strong)UIButton *lastBtn;
@property (nonatomic,copy)doneBlock done;
@property (nonatomic,strong)NSMutableArray *imgViewArray;
@property (nonatomic,strong)NSMutableArray *labelArray;
@property (nonatomic,strong)NSDictionary *param;
@property (nonatomic,copy)NSArray *titleArray;
@end

@implementation CampusListTypeHeaderView

- (NSMutableArray *)imgViewArray{
    if (!_imgViewArray) {
        _imgViewArray = [NSMutableArray array];
    }
    return _imgViewArray;
}
- (NSMutableArray *)labelArray{
    if (!_labelArray) {
        _labelArray = [NSMutableArray array];
    }
    return _labelArray;
}

- (instancetype)initWithCompleteBlock:(void (^)(NSInteger index))completeBlock{
    if (self = [super init]) {
        [self setUI];
        if (completeBlock) {
            self.done = ^(NSInteger index) {
                completeBlock(index);
            };
        }
    }
    return self;
}

- (void)setUI{
    
    self.backgroundColor = [UIColor whiteColor];
    self.frame = CGRectMake(0, 0, kScreenW, 36);
    
    NSArray *titleArray = @[@"地区",@"类型",@"层次",@"批次"];
    
    self.titleArray = titleArray;
    __block UIView *lastView = nil;
    [titleArray enumerateObjectsUsingBlock:^(NSString *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIView *view = [[UIView alloc]init];
        [self addSubview:view];
        if (lastView == nil) {
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.left.equalTo(self);
                make.width.mas_equalTo(kScreenW/4);
                make.height.mas_equalTo(32);
            }];
        }else{
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self);
                make.left.equalTo(lastView.mas_right);
                make.width.mas_equalTo(kScreenW/4);
                make.height.mas_equalTo(32);
            }];
        }
        UILabel *lb = [[UILabel alloc]init];
        lb.text = obj;;
        lb.textColor = C51;
        lb.font = font(12);
        [view addSubview:lb];
        [lb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(view).offset(-4);
            make.centerY.equalTo(view);
        }];
        
        UIButton *btn = [[UIButton alloc]init];
        btn.tag = idx;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [view addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.bottom.equalTo(view);
        }];
        
        UIImageView *img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"course_check_down_tag"]];
        [view addSubview:img];
        [img mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(view);
            make.left.equalTo(lb.mas_right).offset(4);
        }];
        [self.labelArray addObject:lb];
        [self.imgViewArray addObject:img];
        
        lastView = view;
    }];
  
}


- (void)btnClick:(UIButton *)button{
    
    if (self.lastBtn == nil) {
        UIImageView *imgView = self.imgViewArray[button.tag];
        imgView.transform = CGAffineTransformMakeRotation(M_PI);
        self.lastBtn = button;
    }else{
        if (self.lastBtn.tag == button.tag) {
            UIImageView *imgView = self.imgViewArray[button.tag];
            imgView.transform = CGAffineTransformIdentity;
            self.lastBtn = nil;
        }else{
            UIImageView *lastImgView = self.imgViewArray[self.lastBtn.tag];
            lastImgView.transform = CGAffineTransformIdentity;
            
            UIImageView *imgView = self.imgViewArray[button.tag];
            imgView.transform = CGAffineTransformMakeRotation(M_PI);
            
            self.lastBtn = button;
        }
    }
   
    
    self.done(button.tag);
}

- (void)updateModel:(CampusListHeaderModel *)model{
    

    if (model.currentIndex == self.lastBtn.tag) {
        UIImageView *imgView = self.imgViewArray[self.lastBtn.tag];
        UILabel *lb = self.labelArray[self.lastBtn.tag];
        
        if (model.stauts) {
            imgView.image = [UIImage imageNamed:@"course_check_down_select_tag"];
            lb.text = model.name;
        }else{
            imgView.image = [UIImage imageNamed:@"course_check_down_tag"];
            lb.text = self.titleArray[self.lastBtn.tag];
        }
        lb.textColor = model.stauts?RGBA(86, 128, 250, 1):C51;
        
        imgView.transform = CGAffineTransformIdentity;
        
    }else{
        UIImageView *lastImgView = self.imgViewArray[self.lastBtn.tag];
        lastImgView.transform = CGAffineTransformIdentity;
        
        UILabel *lb = self.labelArray[model.currentIndex];
        lb.text = model.name.length == 0?lb.text:model.name;
        lb.textColor = model.stauts?RGBA(86, 128, 250, 1):C51;
        UIImageView *currentImgView = self.imgViewArray[self.lastBtn.tag];
        currentImgView.image = [UIImage imageNamed:model.stauts?@"course_check_down_select_tag":@"course_check_down_tag"];
    }
    self.lastBtn = nil;
    
}
- (void)resetView{
    UIImageView *imgView = self.imgViewArray[self.lastBtn.tag];
    imgView.transform = CGAffineTransformIdentity;
    
    self.lastBtn = nil;
}

@end
